#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <atomic>
#include <fstream>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class TimeUtil
    {
    public:
        TimeUtil() {}
        ~TimeUtil() {}

        static std::string GetTimeStamp()
        {
            struct timeval t;
            gettimeofday(&t, nullptr);

            return std::to_string(t.tv_sec);
        }
    };

    class FileUtil
    {
    public:
        FileUtil()
        {
        }
        ~FileUtil()
        {
        }
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat t;
            // 获取文件属性成功，则文件存在
            int n = stat(path_name.c_str(), &t);
            if (n < 0)
            {
                return false;
            }

            return true;
        }
        static std::string UniqFileName()
        {
            struct timeval t;
            gettimeofday(&t, nullptr);
            int m_sec = t.tv_sec * 1000 + t.tv_usec / 1000;
            static std::atomic<int> x(0);
            x++;
            // 原子性计数器TODO
            std::string file_name = std::to_string(m_sec) + "_" + std::to_string(x);
            return file_name;
        }

        static bool WriteFile(const std::string &target, const std::string &content)
        {
            std::ofstream out(target);
            if (!out.is_open())
                return false;
            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }

        // target : 文件路径
        // content : 输出型，文件内容
        // keep：是否保持内容原貌 
        static bool ReadFile(const std::string &target, std::string *content, bool keep)
        {
            (*content).clear();
            std::ifstream in(target);
            if (!in.is_open())
                return false;
            std::string line;
            while (std::getline(in, line))
            {
                *content += line;
                if (keep)
                    *content += '\n';
            }
            in.close();
            return true;
        }
    };

    const std::string temp_path = "./temp/";

    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;

            return path_name;
        }

        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string CompileError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_err");
        }
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string &src, std::vector<std::string> *target, const std::string &sep)
        {
            boost::split(*target, src, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

} // namespace ns_util
